package com._51cto.healthy.controller;

import com._51cto.healthy.content.P;
import com._51cto.healthy.content.R;
import com._51cto.healthy.excel.ExcelCheckItem;
import com._51cto.healthy.entity.ExcelCheckItemListener;
import com._51cto.healthy.pojo.CheckItem;
import com._51cto.healthy.service.CheckItemService;
import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

@Api(value = "检测项管理" , tags = "预约管理")
@CrossOrigin
@RestController
@RequestMapping("/check-item")
public class CheckItemController {

    @DubboReference
    private CheckItemService checkItemService;

    @ApiOperation(value = "分页查询", notes = "检测项信息-分页获取", tags = "检测项管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "query",value = "检测项名称or检测项编码"),
            @ApiImplicitParam(name = "pageNum",value = "当前页数",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "每页数据",defaultValue = "10")
    })
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @GetMapping("/page/{pageSize}/{pageNum}")
    public R queryPage(@RequestParam(value = "query",required = false/*表示参数不是必填项*/) String query,
                       @PathVariable("pageNum") Integer pageNum,
                       @PathVariable("pageSize") Integer pageSize){
        P<CheckItem> checkItemP = checkItemService.queryPage(query,pageNum,pageSize);
        return R.success(checkItemP);
    }

    @ApiOperation(value = "检测项删除", notes = "检测项信息-主键删除", tags = "检测项管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pk",value = "检测项数据主键"),
    })
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @DeleteMapping("/{id}")
    public R deleteCheckItem(@PathVariable("id") Integer pk){
        checkItemService.deleteByPK(pk);
        return R.success("删除成功");
    }

    @ApiOperation(value = "检测项新增", notes = "新增检测项信息", tags = "检测项管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "chackitem",value = "检测项数据Json"),
    })
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    @PostMapping()
    public R addCheckItem(@RequestBody CheckItem chackitem){
        checkItemService.insert(chackitem);
        return R.success("添加成功");
    }

    @ApiOperation(value = "检测项修改", notes = "编辑检测项信息", tags = "检测项管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "chackitem",value = "检测项数据Json"),
    })
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    @PutMapping()
    public R updateCheckItem(@RequestBody CheckItem checkitem){
        checkItemService.update(checkitem);
        return R.success("更新成功");
    }

    @RequestMapping("/template")
    public void template(HttpServletResponse response) throws IOException {
        //--------读取模板文件
        File template = ResourceUtils.getFile("classpath:static/检测项模板.xlsx");
        FileInputStream input = new FileInputStream(template);
        byte[] buffer = new byte[input.available()];
        input.read(buffer);
        input.close();
        //------- 设置下载响应头
        response.reset();
        response.setContentType("application/octet-stream; charset=UTF-8");
        response.setHeader("Content-length",""+template.length());
        response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(template.getName(), "UTF-8"));
        //-------将结果写出
        OutputStream out = new BufferedOutputStream(response.getOutputStream());
        out.write(buffer);
        out.flush();
    }

    @SneakyThrows
    @PostMapping("/add-list")
    public R addList(MultipartFile file){
        InputStream in = file.getInputStream();
        EasyExcel.read(in, ExcelCheckItem.class,new ExcelCheckItemListener(checkItemService))
                .sheet().doRead();
        return R.success();
    }
}
